#######################
#Team Analysis Code#####
######################

#####################
###Group 4 members#####
#Michelle Lin  ########
#Carlos Galian   ######
#Adanna Chukwuma   ####
#######################s

###############################################################################################
####Cash or Condition: Evidence from a Cash Transfer Experiment################################
####By Sarah Baird, Craig McIntosh, and Berk Ozler#############################################
#### The Quarterly Journal of Economics(2011) 126, 1709 -1753###################################
###############################################################################################
#######################
#Pre-analysis code##
#######################

#Set working directory
setwd("C:/Users/adc785/Dropbox/Replication- Cash or Conditional/Final Code Second Stage")
library(FindIt)
library(plyr)
library(xtable)
library(gridExtra)
load("dataset.RData")


#FindIt Maketwoway will not take non-numeric arguments
#dataset$monthly_money_T2a<-as.numeric(dataset$monthly_money_T2a)
#class(dataset$monthly_money_T2a)

#dataset$monthly_money_T2b<-as.numeric(dataset$monthly_money_T2b)
#class(dataset$monthly_money_T2b)

                      

attach(dataset)
dataset$condition_T2a<-as.numeric(dataset$condition_T2a)
dataset$condition_T2b<-as.numeric(dataset$condition_T2b)
#dataset$monthly_money_T2a<-as.numeric(dataset$monthly_money_T2a)
#dataset$monthly_money_T2b<-as.numeric(dataset$monthly_money_T2b)
#dataset$baseline.age<-as.numeric(dataset$baseline.age)
#dataset$baseline.stratum<-as.numeric(dataset$baseline.stratum)
#detach(dataset)


#create dataset for school sample in round 3(refer to Appendix C)
small.data<-subset(dataset,SS.sample.R3!= "NA" & inschool.term1.2009.SS!="NA"& 
                     inschool.term2.2009.SS !="NA" & inschool.term3.2009.SS !="NA"  
                  & interm1 !="NA" & interm2 !="NA" & interm3!="NA")

#check for missing values
#summary(dataset)
#summary(small.data)

dataC<-subset(dataset, condition_T2a!="NA" & asset.index.baseline!="NA") #Takes out other treatment group
dataU<-subset(dataset, condition_T2b!="NA" & asset.index.baseline!="NA") #Takes out other treatment group
small.dataC<-subset(small.data, condition_T2a !="NA" & asset.index.baseline !="NA")
small.dataU<-subset(small.data, condition_T2b !="NA" & asset.index.baseline != "NA")


data1C<-subset(dataC, inschool.term1.2008.SS!="NA")
data2C<-subset(dataC, inschool.term2.2008.SS!="NA")
data3C<-subset(dataC, inschool.term3.2008.SS!="NA")

data1U<-subset(dataU, inschool.term1.2008.SS!="NA")
data2U<-subset(dataU, inschool.term2.2008.SS!="NA")
data3U<-subset(dataU, inschool.term3.2008.SS!="NA")

small.dataC1<-subset(small.dataC, inschool.term1.2010.SS!="NA")
small.dataU1<-subset(small.dataU, inschool.term1.2010.SS!="NA")
#no missing values for Terms in 2009 so use small.dataU and small.dataC

save(data1C, file="data1C.RData")
save(data2C, file="data2C.RData")
save(data3C, file="data3C.RData")

save(data1U, file="data1U.RData")
save(data2U, file="data2U.RData")
save(data3U, file="data3U.RData")

save(small.dataC, file="small.dataC.RData")
save(small.dataU,file="small.dataU.RData")
save(small.dataC1, file="small.dataC1.RData")
save(small.dataU1,file="small.dataU1.RData")

rm(small.data)
rm(small.dataC)
rm(small.dataU)
rm(dataC)
rm(dataU)
rm(dataset)
rm(data1C)
rm(data2C)
rm(data3C)
rm(data1U)
rm(data2U)
rm(data3U)
rm(small.dataC1)
rm(small.dataU1)

#######################################
#ANALYSIS OF HETEROGENEITY USING FINDIT               
#######################################

HET.FUN<- function(outcome, treat, wgt){


covar.orig<-cbind(age.R1, asset.index.baseline,
                  highest.grade.baseline)

covar.orig<-covar.orig[complete.cases(covar.orig),]
  
covar<-maketwoway(covar.orig, center=T, wts=wgt)
  
  
y<-2*(outcome) - 1# outcome is -1, +1 variable constructed from binary 0, 1 variable
  
  
  
  F1<-FindIt(y=y, #initial analysis to find tuning parameters
             X.c=covar$X, 
             treat=treat, 
             type="single", 
             rescale.c=FALSE , 
             scale.c=covar$s, 
             search.lambdas=TRUE,
             fit.glmnet=T, 
             wts=wgt)
  
  lams<-c(F1$lambdas)#inserts tuning parameters into the model to obtain coefficients
  
  F1<-FindIt(y=y, 
             X.c=covar$X, 
             treat=treat, 
             type="single", 
             rescale.c=FALSE , 
             scale.c=covar$s, 
             search.lambdas=F,
             lambda=lams,
             fit.glmnet=T, 
             wts=wgt)
  

ldply (highlow(F1, X0=covar.orig), data.frame)
}

#####################TEACHER-REPORTED OUTCOMES ################################
############################################################################

#CCT
load("data1C.RData")
attach(data1C)
model.t1CCT<- HET.FUN(outcome=inschool.term1.2008.SS, treat=condition_T2a, wgt=wgt)
rm(data1C)


load("data2C.RData")
attach(data2C)
model.t2CCT<- HET.FUN(outcome=inschool.term2.2008.SS, treat=condition_T2a, wgt=wgt)
rm(data2C)

load("data3C.RData")
attach(data3C)
model.t3CCT<- HET.FUN(outcome=inschool.term3.2008.SS, treat=condition_T2a, wgt=wgt)
rm(data3C)


load("small.dataC.RData")
attach(small.dataC)
model.t4CCT<- HET.FUN(outcome=inschool.term1.2009.SS, treat=condition_T2a, wgt=wgt.SSR3)
model.t5CCT<- HET.FUN(outcome=inschool.term2.2009.SS, treat=condition_T2a, wgt=wgt.SSR3)
model.t6CCT<- HET.FUN(outcome=inschool.term3.2009.SS, treat=condition_T2a, wgt=wgt.SSR3)
rm(small.dataC)

load("small.dataC1.RData")
attach(small.dataC1)
model.t7CCT<- HET.FUN(outcome=inschool.term1.2010.SS, treat=condition_T2a, wgt=wgt.SSR3)
rm(small.dataC1)


#UCT
load("data1U.RData")
attach(data1U)
model.t1UCT<- HET.FUN(outcome=inschool.term1.2008.SS, treat=condition_T2b, wgt=wgt)
rm(data1U)


load("data2U.RData")
attach(data2U)
model.t2UCT<- HET.FUN(outcome=inschool.term2.2008.SS, treat=condition_T2b, wgt=wgt)
rm(data2U)


load("data3U.RData")
attach(data3U)
model.t3UCT<- HET.FUN(outcome=inschool.term3.2008.SS, treat=condition_T2b, wgt=wgt)
rm(data3U)


load("small.dataU.RData")
attach(small.dataU)
model.t4UCT<- HET.FUN(outcome=inschool.term1.2009.SS, treat=condition_T2b, wgt=wgt.SSR3)
model.t5UCT<- HET.FUN(outcome=inschool.term2.2009.SS, treat=condition_T2b, wgt=wgt.SSR3)
model.t6UCT<- HET.FUN(outcome=inschool.term3.2009.SS, treat=condition_T2b, wgt=wgt.SSR3)
rm(small.dataU)

load("small.dataU1.RData")
attach(small.dataU1)
model.t7UCT<- HET.FUN(outcome=inschool.term1.2010.SS, treat=condition_T2b, wgt=wgt.SSR3)
rm(small.dataU1)


save(model.t1CCT, file="model.t1CCT.Rda")
save(model.t2CCT, file="model.t2CCT.Rda")
save(model.t3CCT, file="model.t3CCT.Rda")
save(model.t4CCT, file="model.t4CCT.Rda")
save(model.t5CCT, file="model.t5CCT.Rda")
save(model.t6CCT, file="model.t6CCT.Rda")
save(model.t7CCT, file="model.t7CCT.Rda")

save(model.t1UCT, file="model.t1UCT.Rda")
save(model.t2UCT, file="model.t2UCT.Rda")
save(model.t3UCT, file="model.t3UCT.Rda")
save(model.t4UCT, file="model.t4UCT.Rda")
save(model.t5UCT, file="model.t5UCT.Rda")
save(model.t6UCT, file="model.t6UCT.Rda")
save(model.t7UCT, file="model.t7UCT.Rda")

load("model.t1CCT.Rda")
load("model.t2CCT.Rda")
load("model.t3CCT.Rda")
load("model.t4CCT.Rda")
load("model.t5CCT.Rda")
load("model.t6CCT.Rda")
load("model.t7CCT.Rda")

load("model.t1UCT.Rda")
load("model.t2UCT.Rda")
load("model.t3UCT.Rda")
load("model.t4UCT.Rda")
load("model.t5UCT.Rda")
load("model.t6UCT.Rda")
load("model.t7UCT.Rda")

Term1CCT<-round(xtable(model.t1CCT[c(-1)]), digits=3)
pdf("Term1CCT.pdf", height=11, width=8.5)
grid.table(Term1CCT)
dev.off()

Term2CCT<-round(xtable(model.t2CCT[c(-1)]), digits=3)
pdf("Term2CCT.pdf", height=11, width=8.5)
grid.table(Term2CCT)
dev.off()

Term3CCT<-round(xtable(model.t3CCT[c(-1)]), digits=3)
pdf("Term3CCT.pdf", height=11, width=8.5)
grid.table(Term3CCT)
dev.off()

Term4CCT<-round(xtable(model.t4CCT[c(-1)]), digits=3)
pdf("Term4CCT.pdf", height=11, width=8.5)
grid.table(Term4CCT)
dev.off()

Term5CCT<-round(xtable(model.t5CCT[c(-1)]), digits=3)
pdf("Term5CCT.pdf", height=11, width=8.5)
grid.table(Term5CCT)
dev.off()


Term6CCT<-round(xtable(model.t6CCT[c(-1)]), digits=3)
pdf("Term6CCT.pdf", height=11, width=8.5)
grid.table(Term6CCT)
dev.off()

Term7CCT<-round(xtable(model.t7CCT[c(-1)]), digits=3)
pdf("Term7CCT.pdf", height=11, width=8.5)
grid.table(Term7CCT)
dev.off()

Term1UCT<-round(xtable(model.t1UCT[c(-1)]), digits=3)
pdf("Term1UCT.pdf", height=11, width=8.5)
grid.table(Term1UCT)
dev.off()

Term2UCT<-round(xtable(model.t2UCT[c(-1)]), digits=3)
pdf("Term2UCT.pdf", height=11, width=8.5)
grid.table(Term2UCT)
dev.off()

Term3UCT<-round(xtable(model.t3UCT[c(-1)]), digits=3)
pdf("Term3UCT.pdf", height=11, width=8.5)
grid.table(Term3UCT)
dev.off()

Term4UCT<-round(xtable(model.t4UCT[c(-1)]), digits=3)
pdf("Term4UCT.pdf", height=11, width=8.5)
grid.table(Term4UCT)
dev.off()

Term5UCT<-round(xtable(model.t5UCT[c(-1)]), digits=3)
pdf("Term5UCT.pdf", height=11, width=8.5)
grid.table(Term5UCT)
dev.off()


Term6UCT<-round(xtable(model.t6UCT[c(-1)]), digits=3)
pdf("Term6UCT.pdf", height=11, width=8.5)
grid.table(Term6UCT)
dev.off()

Term7UCT<-round(xtable(model.t7UCT[c(-1)]), digits=3)
pdf("Term7UCT.pdf", height=11, width=8.5)
grid.table(Term7UCT)
dev.off()

###########################################################################
#                         CARLOS GRAPHS
# UCT
lowest.t1UCT<-xtable(arrange(model.t1UCT[11:20,2:5],Estimate))
highest.t1UCT<-xtable(model.t1UCT[1:10,2:5])
lowest.t2UCT<-xtable(arrange(model.t2UCT[11:20,2:5],Estimate))
highest.t2UCT<-xtable(model.t2UCT[1:10,2:5])
lowest.t3UCT<-xtable(arrange(model.t3UCT[11:20,2:5],Estimate))
highest.t3UCT<-xtable(model.t3UCT[1:10,2:5])
lowest.t4UCT<-xtable(arrange(model.t4UCT[11:20,2:5],Estimate))
highest.t4UCT<-xtable(model.t4UCT[1:10,2:5])
lowest.t5UCT<-xtable(arrange(model.t5UCT[11:20,2:5],Estimate))
highest.t5UCT<-xtable(model.t5UCT[1:10,2:5])
lowest.t6UCT<-xtable(arrange(model.t6UCT[11:20,2:5],Estimate))
highest.t6UCT<-xtable(model.t6UCT[1:10,2:5])
lowest.t7UCT<-xtable(arrange(model.t7UCT[11:20,2:5],Estimate))
highest.t7UCT<-xtable(model.t7UCT[1:10,2:5])

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$command <- c('Highest effects & & & & \\\\\n', 
                      '& Age & Assets  & Grade at baseline & Estimated effect\\\\\n',
                      '& (13 to 22) & (-3.69 to 6.83) & (3 to 12) & on enrollment \\\\\n')
print(xtable(highest.t1UCT, caption = 'Highest and lowest effects in term 1 2008',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t2UCT, caption = 'Highest and lowest effects in term 2 2008',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t3UCT, caption = 'Highest and lowest effects in term 3 2008',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t4UCT, caption = 'Highest and lowest effects in term 1 2009',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t5UCT, caption = 'Highest and lowest effects in term 2 2009',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t6UCT, caption = 'Highest and lowest effects in term 3 2009',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t7UCT, caption = 'Highest and lowest effects in term 1 2010',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)


addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$command <- c('Lowest effects & & & & \\\\\n', 
                      '& Age & Assets  & Grade at baseline & Estimated effect\\\\\n',
                      '& (13 to 22) & (-3.69 to 6.83) & (3 to 12) & on enrollment\\\\\n')
print(xtable(lowest.t1UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t2UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t3UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t4UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t5UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t6UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t7UCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)

# CCT
lowest.t1CCT<-xtable(arrange(model.t1CCT[11:20,2:5],Estimate))
highest.t1CCT<-xtable(model.t1CCT[1:10,2:5])
lowest.t2CCT<-xtable(arrange(model.t2CCT[11:20,2:5],Estimate))
highest.t2CCT<-xtable(model.t2CCT[1:10,2:5])
lowest.t3CCT<-xtable(arrange(model.t3CCT[11:20,2:5],Estimate))
highest.t3CCT<-xtable(model.t3CCT[1:10,2:5])
lowest.t4CCT<-xtable(arrange(model.t4CCT[11:20,2:5],Estimate))
highest.t4CCT<-xtable(model.t4CCT[1:10,2:5])
lowest.t5CCT<-xtable(arrange(model.t5CCT[11:20,2:5],Estimate))
highest.t5CCT<-xtable(model.t5CCT[1:10,2:5])
lowest.t6CCT<-xtable(arrange(model.t6CCT[11:20,2:5],Estimate))
highest.t6CCT<-xtable(model.t6UCT[1:10,2:5])
lowest.t7CCT<-xtable(arrange(model.t7CCT[11:20,2:5],Estimate))
highest.t7CCT<-xtable(model.t7CCT[1:10,2:5])

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$command <- c('Highest effects & & & & \\\\\n', 
                      '& Age & Assets  & Grade at baseline & Estimated effect\\\\\n',
                      '& (13 to 22) & (-3.69 to 6.83) & (3 to 12) & on enrollment \\\\\n')
print(xtable(highest.t1CCT, caption = 'Highest and lowest effects of the CCT in term 1 2008',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t2CCT, caption = 'Highest and lowest effects of the CCT in term 2 2008',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t3CCT, caption = 'Highest and lowest effects of the CCT in term 3 2008',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t4CCT, caption = 'Highest and lowest effects of the CCT in term 1 2009',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t5CCT, caption = 'Highest and lowest effects of the CCT in term 2 2009',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t6CCT, caption = 'Highest and lowest effects of the CCT in term 3 2009',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)
print(xtable(highest.t7CCT, caption = 'Highest and lowest effects of the CCT in term 1 2010',digits=c(0,0,2,0,2),caption.placement = 'top'),include.colnames = FALSE,add.to.row = addtorow
)


addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$command <- c('Lowest effects & & & & \\\\\n', 
                      '& Age & Assets  & Grade at baseline & Estimated effect\\\\\n',
                      '& (13 to 22) & (-3.69 to 6.83) & (3 to 12) & on enrollment\\\\\n')
print(xtable(lowest.t1CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t2CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t3CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t4CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t5CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t6CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)
print(xtable(lowest.t7CCT,digits=c(0,0,2,0,2)), 
      add.to.row = addtorow, include.colnames = FALSE)